<?php
class Amounts_float_3d
{

    public $table_name = "amounts_float";
    public $db;
    public $tbl;
    public $cateArr = array("颁奖结算", "<font color=\"green\">储值</font>", "<font color=\"green\">结清</font>");
    public $gtypeStrArr = array("六合彩", "福/体 彩", "时时彩");

    public function Amounts_float_3d ()
    {
        $this->db = new DB();
    }

    public function countBet ($lotto_id, $tbl, $open = "", $x = "")
    {
        $paperOpen = "";
        if ($open == "")
        {
            $sql = "SELECT open FROM lotto_{$tbl} WHERE id = {$lotto_id} LIMIT 1";
            $result = $this->db->query($sql);
            if ($row = mysql_fetch_array($result))
            {
                $paperOpen = $row[0];
            }
        }
        else
        {
            $paperOpen = $open;
        }
        if ($paperOpen != "")
        {
            $now = date("Y-m-d H:i:s");
            $sql = "UPDATE {$this->table_name} SET amount = 0, uptime = '{$now}' WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3";
            $result = $this->db->query($sql);
            $sql = "!!SELECT a.user_id as user_id, sum(a.b1) as amount02, c.amount_3s1 as amount_3s1 FROM {$x}bet_3s1 a ";
            $sql .= " LEFT JOIN lotto_3s1 b ON (a.lotto_id = b.id)";
            $sql .= " LEFT JOIN k_user c ON (a.user_id = c.id)";
            $sql .= " WHERE b.open='{$paperOpen}'";
            $sql .= " AND c.amount_type = 1";
            $sql .= " AND a.rame != 2 ";
            $sql .= " GROUP BY a.user_id";
            $sql .= " UNION ALL ";
            $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02, c.amount_3s1 as amount_3s1 FROM {$x}bet_3s2 a ";
            $sql .= " LEFT JOIN lotto_3s2 b ON (a.lotto_id = b.id)";
            $sql .= " LEFT JOIN k_user c ON (a.user_id = c.id)";
            $sql .= " WHERE b.open='{$paperOpen}'";
            $sql .= " AND c.amount_type = 1";
            $sql .= " AND a.rame != 2";
            $sql .= " GROUP BY a.user_id";
            $sql .= " UNION ALL ";
            $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02, c.amount_3s1 as amount_3s1 FROM {$x}bet_3s3 a ";
            $sql .= " LEFT JOIN lotto_3s3 b ON (a.lotto_id = b.id)";
            $sql .= " LEFT JOIN k_user c ON (a.user_id = c.id)";
            $sql .= " WHERE b.open='{$paperOpen}'";
            $sql .= " AND c.amount_type = 1";
            $sql .= " AND a.rame != 2";
            $sql .= " GROUP BY a.user_id";
            $sql .= " UNION ALL ";
            $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02, c.amount_3s1 as amount_3s1 FROM {$x}bet_3s4 a ";
            $sql .= " LEFT JOIN lotto_3s4 b ON (a.lotto_id = b.id)";
            $sql .= " LEFT JOIN k_user c ON (a.user_id = c.id)";
            $sql .= " WHERE b.open='{$paperOpen}'";
            $sql .= " AND c.amount_type = 1";
            $sql .= " AND a.rame != 2";
            $sql .= " GROUP BY a.user_id";
            $sql .= " UNION ALL ";
            $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02, c.amount_3s1 as amount_3s1 FROM {$x}bet_5h a ";
            $sql .= " LEFT JOIN lotto_5h b ON (a.lotto_id = b.id)";
            $sql .= " LEFT JOIN k_user c ON (a.user_id = c.id)";
            $sql .= " WHERE b.open='{$paperOpen}'";
            $sql .= " AND c.amount_type = 1";
            $sql .= " AND a.rame != 2";
            $sql .= " GROUP BY a.user_id";
            $sql .= " UNION ALL ";
            $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02, c.amount_3s1 as amount_3s1 FROM {$x}bet_8h a ";
            $sql .= " LEFT JOIN lotto_8h b ON (a.lotto_id = b.id)";
            $sql .= " LEFT JOIN k_user c ON (a.user_id = c.id)";
            $sql .= " WHERE b.open='{$paperOpen}'";
            $sql .= " AND c.amount_type = 1";
            $sql .= " AND a.rame != 2";
            $sql .= " GROUP BY a.user_id";
            $result = $this->db->query($sql);
            $idAmuArr = array();
            while ($row = mysql_fetch_array($result))
            {
                $amount = $row['amount02'];
                $user_id = $row['user_id'];
                $sql = "UPDATE {$this->table_name}";
                $sql .= " SET amount = amount + {$amount}";
                $sql .= " , uptime = '{$now}' WHERE user_id = {$user_id} AND paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3";
                $result_2 = $this->db->query($sql);
                if (mysql_affected_rows($this->db->handle_master) == 0)
                {
                    $amount_3s1 = $row['amount_3s1'] * ( 0 - 1 );
                    $sql = "INSERT INTO {$this->table_name} ( user_id, gtype, amount, paperopen, cate, uptime, myamount) VALUES({$user_id}, 3, {$amount}, '{$paperOpen}', 0, '{$now}', {$amount_3s1}) ";
                    $this->db->query($sql);
                }
            }
        }
    }

    public function countBetAgain ($paperOpen, $x = "")
    {
        $now = date("Y-m-d H:i:s");
        $sql = "UPDATE {$this->table_name} SET amount = 0, uptime = '{$now}' WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3";
        $result = $this->db->query($sql);
        $sql = "!!SELECT a.user_id as user_id, sum(a.b1) as amount02 FROM {$x}bet_3s1 a ";
        $sql .= " LEFT JOIN lotto_3s1 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE b.open = '{$paperOpen}'";
        $sql .= " AND a.rame != 2 ";
        $sql .= " AND a.user_id IN( SELECT DISTINCT user_id FROM {$this->table_name} WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3)";
        $sql .= " GROUP BY a.user_id";
        $sql .= " UNION ALL ";
        $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02 FROM {$x}bet_3s2 a ";
        $sql .= " LEFT JOIN lotto_3s2 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE b.open='{$paperOpen}'";
        $sql .= " AND a.rame != 2";
        $sql .= " AND a.user_id IN( SELECT DISTINCT user_id FROM {$this->table_name} WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3)";
        $sql .= " GROUP BY a.user_id";
        $sql .= " UNION ALL ";
        $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02 FROM {$x}bet_3s3 a ";
        $sql .= " LEFT JOIN lotto_3s3 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE b.open='{$paperOpen}'";
        $sql .= " AND a.rame != 2";
        $sql .= " AND a.user_id IN( SELECT DISTINCT user_id FROM {$this->table_name} WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3)";
        $sql .= " GROUP BY a.user_id";
        $sql .= " UNION ALL ";
        $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02 FROM {$x}bet_3s4 a ";
        $sql .= " LEFT JOIN lotto_3s4 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE b.open='{$paperOpen}'";
        $sql .= " AND a.rame != 2";
        $sql .= " AND a.user_id IN( SELECT DISTINCT user_id FROM {$this->table_name} WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3)";
        $sql .= " GROUP BY a.user_id";
        $sql .= " UNION ALL ";
        $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02 FROM {$x}bet_5h a ";
        $sql .= " LEFT JOIN lotto_5h b ON (a.lotto_id = b.id)";
        $sql .= " WHERE b.open='{$paperOpen}'";
        $sql .= " AND a.rame != 2";
        $sql .= " AND a.user_id IN( SELECT DISTINCT user_id FROM {$this->table_name} WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3)";
        $sql .= " GROUP BY a.user_id";
        $sql .= " UNION ALL ";
        $sql .= " SELECT a.user_id as user_id, sum(a.b1) as amount02 FROM {$x}bet_8h a ";
        $sql .= " LEFT JOIN lotto_8h b ON (a.lotto_id = b.id)";
        $sql .= " WHERE b.open='{$paperOpen}'";
        $sql .= " AND a.rame != 2";
        $sql .= " AND a.user_id IN( SELECT DISTINCT user_id FROM {$this->table_name} WHERE paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3)";
        $sql .= " GROUP BY a.user_id";
        $result = $this->db->query($sql);
        $idAmuArr = array();
        while ($row = mysql_fetch_array($result))
        {
            $amount = $row['amount02'];
            $user_id = $row['user_id'];
            $sql = "UPDATE {$this->table_name} SET";
            $sql .= " amount = amount + {$amount}";
            $sql .= ", uptime = '{$now}' WHERE user_id = {$user_id} AND paperopen = '{$paperOpen}' AND cate = 0 AND gtype = 3";
            $result_2 = $this->db->query($sql);
        }
    }

    public function dataListRS ($id, $gtype, $cate = "", $page = 1, $totalPage = 10)
    {
        $sql = "SELECT * FROM {$this->table_name} WHERE gtype = {$gtype} AND user_id = {$id}";
        if ($cate != "-1")
        {
            $sql .= " AND cate = {$cate}";
        }
        $sql .= " ORDER BY paperopen DESC, id DESC";
        return getdb($sql, $page, $totalPage);
    }

    public function getAmountFloat ($id, $gtype = 3, $cate = -1)
    {
        $reVal = 0;
        if ($cate != "-1")
        {
            $sqlAdd = " AND cate = {$cate}";
        }
        else
        {
            $sqlAdd = "";
        }
        $sql = "!!SELECT IFNULL(SUM(amount),0) as amount FROM amounts_float WHERE gtype = {$gtype} AND user_id = {$id} {$sqlAdd}";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function insertData ($user_id, $prev_id, $prev_account, $gtype = 3, $amount = 0)
    {
        $today = date("Y-m-d");
        $uptime = date("Y-m-d H:i:s");
        $sql = "INSERT INTO {$this->table_name} (user_id, prev_id, prev_account, gtype, amount, paperopen, cate, uptime)";
        $sql .= " VALUES({$user_id}, {$prev_id}, '{$prev_account}', {$gtype}, {$amount}, '{$today}', 1, '{$uptime}')";
        $this->db->query($sql);
    }

    public function clrAmountFloat ($user_id, $prev_id, $prev_account, $nowAmount = 0, $gtype = 3)
    {
        $today = date("Y-m-d");
        $uptime = date("Y-m-d H:i:s");
        $sql = "INSERT INTO {$this->table_name} (user_id, prev_id, prev_account, gtype, amount, paperopen, cate, uptime)";
        $sql .= " VALUES({$user_id}, {$prev_id}, '{$prev_account}', {$gtype}, {$nowAmount}, '{$today}', 2, '{$uptime}')";
        echo $sql;
        if ($result = $this->db->query($sql))
        {
            return true;
        }
        return false;
    }

    public function calcClearData ($paperOpen, $gtype = 3)
    {
        $sql = "DELETE a FROM {$this->table_name} a";
        $sql .= ", (SELECT b.user_id FROM (SELECT SUM(amount) as amount, user_id FROM {$this->table_name} WHERE paperopen <= '{$paperOpen}' AND gtype = {$gtype} GROUP BY user_id) as b WHERE b.amount = 0) b";
        $sql .= " WHERE a.gtype = {$gtype} AND b.user_id = a.user_id AND paperopen <= '{$paperOpen}'";
        $result = $this->db->query($sql);
        return mysql_affected_rows();
    }

}

?>